package com.faner.function

import com.faner.bean.LoginResult
import com.fun.frame.httpclient.FanLibrary
import com.fun.utils.RString
import com.fun.utils.Regex
import net.sf.json.JSONObject
import org.apache.http.client.methods.HttpPost
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class GuDan extends FanLibrary {
    private static Logger logger = LoggerFactory.getLogger(GuDan.class)

    static int uid1 = 74705, tid1 = 263610;

    String de = "SM-C7010"
    static String uname = "Never More",
                  rcontent = RString.getRandomString(LoginResult.text), device = "iPhone XS";
    static int uid2 = 10214, tuid2 = 85103, tid2 = 263621;

    static int rid = 1086210, tuid3 = 84956, tid3 = 263100, uid3 = 52486;
    static String tname = "我觉得你的网名很多，北京欢迎你，也是你。你以前的网名是人在杭州。", text = "回复 为赋新词强说愁:都是我，我就是想招惹你", uanme3 = "但是不知道该怎么接近你", device3 = "爱上买买买";

    static int tuid4 = 10214;
    static String uname4 = uname, tname4 = "", tcontent4 = "", device4 = device;

    static Map<Integer, Integer> myList = new HashMap<Integer, Integer>() {
        {
            put(263340, 8308)
            put(263342, 11)
            put(262837, 8308)
            put(262819, 5276)
            put(262869, 4618)
            put(59135, 339)
            put(263285, 84956)
            put(263246, 72153)
            put(263252, 85095)
            put(263249, 85094)
            put(263418, 65294)
            put(262620, 65242)
            put(263247, 85093)
            put(263350, 11619)
            put(263421, 33448)
            put(262536, 11619)
            put(262741, 34275)
            put(263358, 11)
            put(263352, 285)
            put(263359, 4618)
            put(263360, 5276)
            put(263410, 85105)
        }
    }


    static void main(String[] args) {
//        for (int i in 0..20) {
//            up();
//            down();
//            sleep(200);
//        }
        reply1();
//		reply2();
//			getHide(78199);
//		send();
//        refresh()
//        def s = args[0]
//        reply1(changeStringToInt(s))

        testOver()

    }

/**
 * 获取tid对应的uid
 * @param tid
 * @return
 */
    static getUid(int tid) {
        String url = "http://www.gudanxinshi.com/blog/" + tid + "/1"
        def response = getHttpResponse(getHttpGet(url))
//        output(response)
        def string = response.getString("content")
        def all = Regex.regexAll(string, " * +<a href=\"/userSelect\\?uid=\\d+")[0]
        all = all.substring(all.lastIndexOf("=") + 1, all.length())
        return changeStringToInt(all)
    }

/**
 * 刷新首页
 */
    static refresh() {
        def set = myList.keySet()
        def list = new ArrayList<>(set)
        for (int i = 0; i < list.size(); i++) {
            if (getRandomInt(2) == 1) continue
            sleep(10)
            def key = list.get(i)
            def value = myList.get(key)
            reply1(i, value)
        }
    }

    static void getHide(int id) {
        int i = 1;
        while (get(id, i++)) {
            sleep(1)
        }
    }

    static boolean get(int id, int page) {
        String url = "http://1.lonelymind.sinaapp.com/select";
        JSONObject params = new JSONObject();
        params.put("uid", id);
        params.put("op", "myuser");
        params.put("page", page);
        HttpPost httpPost = getHttpPost(url, params);
        JSONObject response = getHttpResponse(httpPost);
//		output(response);
        if (response.toString().length() < 150) return false;
        String content = response.getString("content").replace("\n", EMPTY).replace("\r", EMPTY);
        List<String> list = regexAll(content, "<tname>.*?</tname>");
        List<String> list1 = regexAll(content, "<tcontent>.*?</tcontent>");
        List<String> list2 = regexAll(content, "<tid>.*?</tid>");
        List<String> list3 = regexAll(content, "<kind>.*?</kind>");
        for (int i = 0; i < list.size(); i++) {
            String name = list.get(i).replaceAll("</*tname>", EMPTY);
            String tid = list2.get(i).replaceAll("</*tid>", EMPTY);
            String text = list1.get(i).replaceAll("</*tcontent>", EMPTY);
            String kind = list3.get(i).replaceAll("</*kind>", EMPTY);
            if (kind.equals("-1"))
                output(tid + TAB + name + TAB + text);
        }
        return true;
    }

    static void spider(int id) {
        JSONObject params = new JSONObject();
        params.put("uid", id);
        HttpPost httpPost = getHttpPost("http://1.lonelymind.sinaapp.com/userSelect", params);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
        String content = response.getString("content").replace("\n", EMPTY).replace("\r", EMPTY);
        if (content == null || content.equals("-1") || !content.startsWith("<?xml")) return;
        String uid = regexAll(content, "<uid>\\d+</uid>").get(0);
        String name = regexAll(content, "<uname>.*</uname>").get(0);
        String flower = regexAll(content, "<flower>.+</flower>").get(0);
        String cai = regexAll(content, "<niufun>.*</niufun>").get(0);
        List<String> list = regexAll(content, "<qianming>.*</qianming>");
        String desc = regexAll(content, "<qianming>.*</qianming>").get(0);
        flower = flower.replace("<flower>", EMPTY).replace("</flower>", EMPTY);
        cai = cai.replace("<niufun>", EMPTY).replace("</niufun>", EMPTY);
        uid = uid.replace("<uid>", EMPTY).replace("</uid>", EMPTY);
        name = name.replace("<uname>", EMPTY).replace("</uname>", EMPTY);
        desc = desc.replace("<qianming>", EMPTY).replace("</qianming>", EMPTY);
        String sql = String.format("INSERT INTO spider_gudan (uid,uname,flower,tread,sign)VALUES(%s,\"%s\",%s,%s,\"%s\");", uid, name, flower, cai, desc);
        logger.info(sql)
    }


    static void up(int tid, int i) {
        int uid = getUid(tid)
        up(uid, tid, i)
    }

    static void up(int uid, int tid, int times) {
        String url = "http://1.lonelymind.sinaapp.com/titleUp";
        JSONObject params = new JSONObject();
        params.put("uid", uid);
        params.put("tid", tid);
        for (int i = 0; i < times; i++) {
            getHttpResponse(getHttpPost(url, params));
        }
    }

    static void down(int tid, int times) {
        int uid = getUid(tid)
        down(uid, tid, times)
    }

    static void down(int uid, int tid, int times) {
        String url = "http://1.lonelymind.sinaapp.com/titleDown";
        JSONObject params = new JSONObject();
        params.put("uid", uid);
        params.put("tid", tid);
        for (int i = 0; i < times; i++) {
            getHttpResponse(getHttpPost(url, params));
        }
    }

    /**
     * 回复帖子
     */
    static void reply1() {
        String url = "http://1.lonelymind.sinaapp.com/replyInsert";
        JSONObject params = new JSONObject();
        params.put("uname", uname);
        params.put("rcontent", rcontent);
        params.put("uid", uid2);
        params.put("device", device);
        params.put("tuid", tuid2);
        params.put("tid", tid2);
        JSONObject response = getHttpResponse(getHttpPost(url, params));
        logger.info(response);
    }

    static void reply1(int tid, String content) {
        String url = "http://1.lonelymind.sinaapp.com/replyInsert";
        JSONObject params = new JSONObject();
        params.put("uname", uname);
        params.put("rcontent", content);
        params.put("uid", uid2);
        params.put("device", device);
        params.put("tuid", getUid(tid));
        params.put("tid", tid);
        JSONObject response = getHttpResponse(getHttpPost(url, params));
    }
    /**
     * 回复帖子
     */
    static void reply1(int tid, int tuid) {
        String url = "http://1.lonelymind.sinaapp.com/replyInsert";
        JSONObject params = new JSONObject();
        params.put("uname", uname);
        params.put("rcontent", rcontent);
        params.put("uid", uid2);
        params.put("device", device);
        params.put("tuid", tuid);
        params.put("tid", tid);
        JSONObject response = getHttpResponse(getHttpPost(url, params));
    }
    /**
     * 回复帖子
     */
    static void reply1(int tid) {
        String url = "http://1.lonelymind.sinaapp.com/replyInsert";
        JSONObject params = new JSONObject();
        params.put("uname", uname);
        params.put("rcontent", rcontent);
        params.put("uid", uid2);
        params.put("device", device);
        params.put("tuid", getUid(tid));
        params.put("tid", tid);
        JSONObject response = getHttpResponse(getHttpPost(url, params));
    }
    /**
     * 回复留言
     */
    static void reply2() {
        String url = "http://1.lonelymind.sinaapp.com/freplyInsert";
        JSONObject params = new JSONObject();
        params.put("rid", rid);
        params.put("uid", uid3);
        params.put("uname", uanme3);
        params.put("tname", tname);
        params.put("tid", tid3);
        params.put("text", text);
        params.put("tuid", tuid3);
        params.put("device", device3);
        HttpPost httpPost = getHttpPost(url, params);
        JSONObject response = getHttpResponse(httpPost);
    }

    static void send() {
        String url = "http://1.lonelymind.sinaapp.com/titleInsert";
        JSONObject params = new JSONObject();
        params.put("uname", uname4);
        params.put("kind", 1);
        params.put("tname", tname4);
        params.put("tuid", tuid4);
        params.put("tcontent", tcontent4);
        params.put("device", device4);
        HttpPost httpPost = getHttpPost(url, params);
        JSONObject response = getHttpResponse(httpPost);
    }

    static int getUser(int tid) {
        String url = "http://www.gudanxinshi.com/blog/" + tid + "/1";
        JSONObject response = getHttpResponse(getHttpGet(url));
        String content = response.toString();
        List<String> list = regexAll(content, "uid=\\d+");
        int id = changeStringToInt(list.get(0).substring(4));
        return id;
    }
}
